package com.cgbl.service.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.R
import com.cgbl.service.assist.DictTypeAssist
import com.cgbl.service.databinding.AppActivityMainBinding
import com.cgbl.service.vm.MainViewModel
import com.gyf.immersionbar.ktx.immersionBar
import com.netease.yunxin.kit.chatkit.ui.ChatKitClient
import com.netease.yunxin.kit.chatkit.ui.custom.ChatOrderViewHolder
import com.netease.yunxin.kit.chatkit.ui.custom.ChatStickerViewHolder
import com.netease.yunxin.kit.chatkit.ui.custom.OrderAttachment
import com.netease.yunxin.kit.chatkit.ui.custom.StickerAttachment
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.view.miuibottomnavigation.Mode
import com.robot.base.view.miuibottomnavigation.build.NavigationBuild
import me.goldze.mvvmhabit.BR


@Route(path = AppConstants.Router.Main.A_MAIN)
class MainActivity : GLBaseActivity<AppActivityMainBinding, MainViewModel>() {
    private var fragmentList = mutableListOf<Fragment>()
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_main

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {

        immersionBar {
            transparentStatusBar()
            keyboardEnable(false)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }
        fragmentList.add(HomeFragment())
        fragmentList.add(WorkFragment())
        fragmentList.add(MessageFragment())
        fragmentList.add(MineFragment())
        initConfig()
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
//        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.bottomNavigationView.init(
            NavigationBuild.Builder(this).addItem(
                getString(R.string.app_main_menu_0),
                R.mipmap.app_ic_main_tab_1_un,
                R.mipmap.app_ic_main_tab_1
            ).addItem(
                getString(R.string.app_main_menu_1),
                R.mipmap.app_ic_main_tab_2_un,
                R.mipmap.app_ic_main_tab_2
            ).addItem(
                getString(R.string.app_main_menu_3),
                R.mipmap.app_ic_main_tab_4_un,
                R.mipmap.app_ic_main_tab_4
            ).addItem(
                getString(R.string.app_main_menu_4),
                R.mipmap.app_ic_main_tab_5_un,
                R.mipmap.app_ic_main_tab_5
            ).setMode(Mode.MODE_SCROLL).setupWithViewPager(binding.mViewPager2)
                .setSelectTextColor(Color.RED).setFixedItems(mutableSetOf(1)).build()
        )
        ChatKitClient.addCustomAttach(1001, StickerAttachment::class.java)
        ChatKitClient.addCustomViewHolder(
            1001, ChatStickerViewHolder::class.java
        )
        ChatKitClient.addCustomAttach(1002, OrderAttachment::class.java)
        ChatKitClient.addCustomViewHolder(
            1002, ChatOrderViewHolder::class.java
        )
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        val intent = Intent()
        // 为Intent设置Action、Category属性
        intent.action = Intent.ACTION_MAIN // "android.intent.action.MAIN"
        intent.addCategory(Intent.CATEGORY_HOME) //"android.intent.category.HOME"
        startActivity(intent)
    }

    private fun initConfig() {
        DictTypeAssist.refreshCareerDictType()
    }
}